//
//  MapViewController.m
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 3/14/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "MapViewController.h"
#import "NGUtility.h"
#import "NGLocation.h"

@implementation AddressAnnotation

@synthesize coordinate;
@synthesize mTitle;
@synthesize mSubtitle;

- (NSString *)subtitle{
    return mSubtitle;
}

- (NSString *)title{
    return mTitle;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) coord {
    coordinate = coord;
    mTitle = @"Title";
    mSubtitle = @"Sub title";
    NSLog(@"%f,%f", coord.latitude, coord.longitude);
    return self;
}


-(id)initWithNGLocation:(NGLocation *) location {
    CLLocationCoordinate2D clLocation;
    clLocation.latitude = location.lat;
    clLocation.longitude = location.lon;
    coordinate = clLocation;
    mSubtitle = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%f, %f", location.lat, location.lon]];
    mTitle = [[NSString alloc] initWithString: [NGUtility friendlyStatus:location]];
    return self;
}
-(void)dealloc {
    [mSubtitle release];
    [mTitle release];
    [super dealloc];
}

@end

@implementation MapViewController

@synthesize location;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        nibNameOrNil = [nibNameOrNil stringByAppendingString:@"-ipad"];
    }
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - button actions

- (IBAction) cancel: (id) sender { 
    [self.view removeFromSuperview];
}

#pragma mark - map delegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorGreen;
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    CLLocationCoordinate2D clLocation;
    clLocation.latitude = location.lat;
    clLocation.longitude = location.lon;
    
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;
    
    region.span=span;
    region.center=clLocation;
    if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        [addAnnotation release];
        addAnnotation = nil;
    }
    addAnnotation = [[AddressAnnotation alloc] initWithNGLocation:location];
    [mapView addAnnotation:addAnnotation];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [addAnnotation release];
    addAnnotation = nil;
    [location release];
    [mapView release];
    [super dealloc];
}

@end
